package com.example.news.service;

import com.example.news.bean.User;
import com.example.news.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    /**
     * 登录
     * @param account
     * @param password
     * @return
     */
    public Map<String,Object> login(String account, String password){
        Map<String,Object>  resp = new HashMap<>();
        User user = userMapper.getUserByAccount(account);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                resp.put("code",200);
                resp.put("msg","登陆成功");
                resp.put("data",user);
            }else {
                resp.put("code",201);
                resp.put("msg","密码错误");
            }
        }else {
            resp.put("code",201);
            resp.put("msg","用户不存在");
        }
        return resp;
    }


    /**
     * 注册
     * @return
     */
    public Map<String,Object> register(User user){
        Map<String,Object>  resp = new HashMap<>();
        User userByAccount = userMapper.getUserByAccount(user.getAccount());
        if (userByAccount==null){
            int i = userMapper.insertUserInfo(user);
            if (i > 0){
                resp.put("code",200);
                resp.put("msg","注册成功");
            }else {
                resp.put("code",201);
                resp.put("msg","注册失败");
            }
        }else {
            resp.put("code",201);
            resp.put("msg","该账号已注册");
        }
        return resp;
    }

}
